//index.js
//获取应用实例
const app = getApp()
const quote = app.globalData.quote;
const api = quote.api;
const WXlogin = quote.WXlogin
const mta = require('../../../utils/mta_analysis.js'); //引入腾讯埋点文件

Page({
    data: {
        isLongScrern: app.globalData.isLongScrern,
        isPhoneX: app.globalData.isPhoneX,
        isBrang: app.globalData.isBrang, 
        windowWidth: app.globalData.windowWidth,
        isShowUnlock: false,
        isCanScroll: false,
        OpenId: '',
        list: '',
        isClick: false,
        isHaveData: false,
        isBackNext: false
    },
    clickContact: function () {
        console.log('底部导航-点击客服按钮')
        app.aldstat.sendEvent('底部导航-点击客服按钮');
        mta.Event.stat("bottom_clickService", {});
        
    },
    contact:function(){
        app.aldstat.sendEvent('点击客服按钮');
        mta.Event.stat("click_service", {});
    },
    bindtransitionend: function () {
        console.log('动画结束')
    },
    onLoad: function (options) {
        let that = this

      // var a = 'abcdefghijklmnopqrxtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      // console.log(a.charAt(parseInt(Math.random() * a.length + 4)),'28');
      console.log(797979797979797)
        console.log('判断是否从下个页面返回的', options)
        if (options.goback === 'true') {
            console.log('是从下个页面返回的')
            that.setData({
                isBackNext: true
            })
        }
        wx.showLoading({
            title: '加载中...',
        })
        // var ages = [23, 44, 3]
        // if (ages.some(age => age < 10)) {
        //console.log('')
        // }
        mta.Page.init(); //初始化腾讯埋点

    },
    onReady: function () {

    },
  onShow: function (res) {
        var that = this
        console.log('************', app.globalData.isLongScrern, app.globalData.isBrang)
        wx.getStorage({
            key: 'openid',
            success: function (res) {
                that.getListData()
            },
            fail: function (res) {
                console.log('获取缓存的openid失败-------------')
                app.login().then(function (res) {
                    if (res.status == 200) {
                        console.log(res, '获取的OpenId+++++++++++++++')
                        that.getListData()
                    }
                })
            }
        })
    },
    
    onHide: function () {
        console.log('销毁定时器')
        var that = this
        clearTimeout(that.data.setTime)
    },
    //获取列表数据
    getListData() {
        var that = this
        var dataDic = {
            OpenId: app.globalData.openId,
        };
        api.methodBasicEnglish_UnlockInformation({
            dataDic,
            success: function (res) {
                wx.hideLoading()
                console.log(res, '70')
                that.setData({
                    list: res.data.data.UnitList,
                    isHaveData: true
                })
                that.data.setTime = setTimeout(() => {
                    if (that.data.isBackNext) {

                    } else {
                        that.btnClickTo()
                    }
                }, 2000)
            }
        })
    },
    //前往下一页
    go_list(e) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        var that = this
        if (that.data.isClick) {
            console.log('不要重复点击')
            return
        }
        that.setData({
            isClick: true
        })
        if (app.globalData.userInfo) {
            console.log(e)

            var index = e.currentTarget.dataset.indx
            var status = e.currentTarget.dataset.status
            var unitId = e.currentTarget.dataset.unitid
            var unitNumber = e.currentTarget.dataset.indx + 1
            app.globalData.unitIndex = index
            console.log(app.globalData.unitIndex)
            if (status == 1) {
                app.aldstat.sendEvent('选择单元', {
                    'unit': '解锁状态-第' + unitNumber + '单元'
                });
                mta.Event.stat("unit_click", {
                    'unit': '解锁状态-第' + unitNumber + '单元'
                });
                wx.navigateTo({
                    url: '../../../pages/indexPackage/courseList/courseList'
                })
                that.setData({
                    isClick: false
                })
                wx.hideLoading()
            } else if (status == 0) {
                app.aldstat.sendEvent('选择单元', {
                    'unit': '未解锁状态-第' + unitNumber + '单元'
                });
                mta.Event.stat("unit_click", {
                    'unit': '未解锁状态-第' + unitNumber + '单元'
                });
                that.setData({
                    isShowUnlock: true,
                    isClick: false
                })
                wx.hideLoading()
            }
        } else {
            console.log('没数据')
            var that = this
            wx.hideLoading()
            wx.getUserInfo({
                success(res) {
                    app.globalData.userInfo = res.userInfo
                    wx.setStorage({
                        key: 'userInfo',
                        data: res.userInfo,
                        success(res) {
                            console.log(res, '保存成功')
                            that.setData({
                                isClick: false
                            })
                        },
                    })
                },
                fail() {
                    console.log('****')
                    that.setData({
                        isClick: false
                    })
                }
            })
        }
    },
    // 上去的动画
    btnClickTo() {
        var animation = wx.createAnimation({
            transformOrigin: "50% 50%",
            duration: 3000,
            timingFunction: "ease",
            delay: 0
        })
        console.log(-560 / 750 * this.data.windowWidth)
        this.animation = animation
        animation.translateY(-560 / 750 * this.data.windowWidth).step()
        this.setData({
            animation: animation.export(),
        })
    },
    //前往玩法说明页面
    gotoIntroduce: function () {
        app.aldstat.sendEvent('底部导航-点击玩法说明');
        mta.Event.stat("bottom_clickPlay", {});
        wx.navigateTo({
            url: '/pages/indexPackage/introduce/introduce',
        })
    },
    //未解锁弹窗提示
    confirm: function () {
        var that = this
        that.setData({
            isShowUnlock: false
        })
    },
    onShareAppMessage: function () {
        return {
            path: 'pages/indexPackage/index/index',
            success: function (res) {
                console.log('转发成功', res);
            },
            fail: function (err) {
                console.log('转发失败', err)
            }
        }
     }

})